﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameBase
{
    public static class Texture2DHelper
    {
        public static Texture2D loadTexture2DFromDLL(object obj, string substr)
        {
            string[] resStrings = obj.GetType().Assembly.GetManifestResourceNames();

            //if (resStrings.GetLength(0) > 0)
            foreach (string resStr in resStrings)
            {
                try
                {
                    if (resStr.IndexOf(substr) != -1)
                    {
                        Bitmap bmp = new Bitmap(obj.GetType().Assembly.GetManifestResourceStream(resStr));
                        if (bmp != null)
                            return convertBitmapToTexture2D(GameBase.Constant.graphics.GraphicsDevice, bmp);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return null;
                }
            }
            return null;
        }

        public static Texture2D convertBitmapToTexture2D(GraphicsDevice GraphicsDevice, Bitmap image)
        {
            // Buffer size is size of color array multiplied by 4 because   
            // each pixel has four color bytes  
            int bufferSize = image.Height * image.Width * 4;

            // Create new memory stream and save image to stream so   
            // we don't have to save and read file  
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bufferSize);
            image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

            // Creates a texture from IO.Stream - our memory stream  
            Texture2D texture = Texture2D.FromStream(GraphicsDevice, memoryStream);

            return texture;
        }

        /// <summary>
        /// Creates a new image from an existing image.
        /// </summary>
        /// <param name="bounds">Area to use as the new image.</param>
        /// <param name="source">Source image used for getting a part image.</param>
        /// <returns>Texture2D.</returns>
        public static Texture2D createPartImage(GraphicsDeviceManager graphics, Microsoft.Xna.Framework.Rectangle bounds, Texture2D source)
        {
            //Declare variables
            Texture2D result;
            Microsoft.Xna.Framework.Color[]
                sourceColors,
                resultColors;

            //Setup the result texture
            result = new Texture2D(graphics.GraphicsDevice, bounds.Width, bounds.Height);

            //Setup the color arrays
            sourceColors = new Microsoft.Xna.Framework.Color[source.Height * source.Width];
            resultColors = new Microsoft.Xna.Framework.Color[bounds.Height * bounds.Width];

            //Get the source colors
            source.GetData<Microsoft.Xna.Framework.Color>(sourceColors);

            //Loop through colors on the y axis
            for (int y = bounds.Y; y < bounds.Height + bounds.Y; y++)
            {
                //Loop through colors on the x axis
                for (int x = bounds.X; x < bounds.Width + bounds.X; x++)
                {
                    //Get the current color
                    resultColors[x - bounds.X + (y - bounds.Y) * bounds.Width] =
                        sourceColors[x + y * source.Width];
                }
            }

            //Set the color data of the result image
            result.SetData<Microsoft.Xna.Framework.Color>(resultColors);

            //return the result
            return result;
        }
    }
}
